using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Super
{
	public class ExcelExtension : EditorWindow
	{

		string excelPath = @"";
		ExcelOperateType operType = ExcelOperateType.LoadData;
		bool showContent = false;
		Vector2 scrollPos = Vector2.zero;

		[MenuItem("Framework/ExcelToolWindow")]
		public static void Init()
		{
			ExcelExtension exWindow = GetWindow<ExcelExtension>();
			exWindow.Show();
		}

		public void OnGUI()
		{
			#region
			EditorGUILayout.LabelField("Excel数据与Asset资源处理");
			operType = (ExcelOperateType)GUILayout.Toolbar((int)operType, new[] { "导入新Excel", "重新导入Excel", "移除Excel的导入", "EXCEL_SO文件回写Excel", "其他操作" }, GUILayout.Height(50));

			switch (operType)
			{
				case ExcelOperateType.LoadData:
					RefreshLoadData();
					break;
				case ExcelOperateType.ReloadData:
					CommonAllOper(EditorUtil.LoadAllExcel, "重新导入所有Excel!!!", EditorUtil.LoadExcel, "重新导入");
					break;
				case ExcelOperateType.RemoveData:
					CommonAllOper(EditorUtil.RemoveAllExcelSO, "移除所有Excel的Asset文件，并删除导入记录!!!", EditorUtil.RemoveExcelSO, "移除Asset文件");
					break;
				case ExcelOperateType.WriteToExcel:
					CommonAllOper(EditorUtil.WriteAllExcel, "所有Asset文件写入Excel!!!", EditorUtil.WriteExcel, "写入Excel文件");
					break;
				case ExcelOperateType.OtherOp:
					RefreshOtherOper();
					break;
			}
			#endregion
		}
		
		/// <summary>
		/// 通用操作面板
		/// </summary>
		/// <param name="allOper">所有选项操作回调</param>
		/// <param name="allOperText">所有选项操作按钮文本</param>
		/// <param name="action">单条目操作回调</param>
		/// <param name="btnText">单条目操作按钮文本</param>
		public void CommonAllOper(Action allOper, string allOperText, Action<string> action, string btnText)
		{
			if (GUILayout.Button(allOperText, GUILayout.Height(50)))
			{
				allOper();
			};
			GUILayout.BeginHorizontal();
			GUILayout.Space(10);
			scrollPos = GUILayout.BeginScrollView(scrollPos);
			ShowAllLoadedExcel(action, btnText);
			GUILayout.EndScrollView();
			GUILayout.EndHorizontal();
		}

		/// <summary>
		/// 显示所有已导入Excel条目，并提供按钮操作
		/// </summary>
		/// <param name="action">按钮点击回调</param>
		/// <param name="btnText">按钮文本</param>
		public void ShowAllLoadedExcel(Action<string> action, string btnText)
		{
			for (int i = 0; i < ExcelNoteSO.Ins.Notes.Count; i++)
			{
				string excelPath = ExcelNoteSO.Ins.Notes[i].excelPath;
				GUILayout.BeginHorizontal(GUILayout.MinHeight(50));
				GUILayout.BeginVertical(GUILayout.Width(500));
				GUILayout.Space(5);
				EditorGUILayout.LabelField("Excel表格资源路径:	" + excelPath);
				GUILayout.EndVertical();
				GUILayout.FlexibleSpace();
				if (GUILayout.Button(btnText, GUILayout.Width(100), GUILayout.ExpandHeight(true)))
				{
					action(excelPath);
				};
				GUILayout.Space(15);

				GUILayout.EndHorizontal();
				GUILayout.Box("", GUILayout.Height(5), GUILayout.ExpandWidth(true));
			}
		}

		/// <summary>
		/// 显示通用Excel导入格式要求
		/// </summary>
		public void RefreshCommonLoad()
		{
			GUIStyle customLabelStyle = new GUIStyle(EditorStyles.label);
			customLabelStyle.normal.textColor = Color.red;
			GUILayout.Space(5);

			showContent = EditorGUILayout.Foldout(showContent, "Excel格式要求:\t");
			if (showContent)
			{
				EditorGUILayout.LabelField("	字段名称以[K]开头代表主键", customLabelStyle);
				EditorGUILayout.LabelField("	字段valid、信息是否有效、int类型：1有效", customLabelStyle);
				EditorGUILayout.LabelField("	第一行设置字段的备注，阐述字段的作用等");
				EditorGUILayout.LabelField("	第二行设置字段名称：小写开头的驼峰命名法，例: systemId、name，fightValue等");
				EditorGUILayout.LabelField("	第三行设置字段的数据类型：例如基本数据：int，string，char，float，double，long，ulong，short，ushort。\n\t\t\t\t枚举格式：enum+枚举名称，例如：enumDialoguePos", customLabelStyle, GUILayout.Height(40));
				EditorGUILayout.LabelField("	第一列设置当前行信息的备注");
			}


			GUILayout.Space(5);
			EditorGUILayout.LabelField("Excel资源路径案例:	" + @"Assets/FrameWork/ExcelTool/ExcelSample.xlsx");
			GUILayout.Box("", GUILayout.Height(5), GUILayout.ExpandWidth(true));
			EditorGUILayout.BeginHorizontal();
			excelPath = EditorGUILayout.TextField("Excel资源路径", excelPath);
			if (GUILayout.Button("选择表格", GUILayout.Width(80)))
			{
				string directory = GlobalUtil.GetAbsolutePath("Assets");
				string path = EditorUtility.OpenFilePanel("选择Excel表格文件", directory, "xlsx");
				excelPath = GlobalUtil.GetAssetsRelativePath(path);
			}
			EditorGUILayout.EndHorizontal();
			GUILayout.Space(10);
		}

		/// <summary>
		/// 显示Excel导入信息
		/// </summary>
		public void RefreshLoadData()
		{
			RefreshCommonLoad();
			GUILayout.Space(10);
			if (ExcelNoteSO.Ins.CheckIfSetTableInfo(excelPath))
			{
				EditorGUILayout.LabelField("已生成并编译结构体");
			}
			if (ExcelNoteSO.Ins.CheckIfSetTableSO(excelPath))
			{
				EditorGUILayout.LabelField("导入Excel数据");
			}
			GUILayout.Space(10);
			GUILayout.BeginHorizontal();
			if (GUILayout.Button("生成并编译结构体", GUILayout.Height(50)))
			{
				EditorUtil.GenerateExcelStructure(excelPath);
			}
			if (GUILayout.Button("导入Excel数据", GUILayout.Height(50)))
			{
				EditorUtil.LoadExcel(excelPath);
			}
			GUILayout.EndHorizontal();
		}

		/// <summary>
		/// 刷新其他杂项操作界面
		/// </summary>
		public void RefreshOtherOper()
		{
			RefreshCommonLoad();
			GUILayout.Space(10);
			GUILayout.BeginHorizontal();
			if (GUILayout.Button("生成枚举", GUILayout.Height(50)))
			{
				EditorUtil.GenerateExcelEnum(excelPath);
			}
			GUILayout.EndHorizontal();
			GUILayout.Space(10);
			GUILayout.Box("", GUILayout.Height(5), GUILayout.ExpandWidth(true));
			GUILayout.Space(10);
			if (GUILayout.Button("生成ExcelLangKeys", GUILayout.Height(50)))
			{
				LangUtil.GenerateAllExcelFile();
			}
		}
	}

}
